﻿requirejs.config({
    baseUrl: ".",
    paths: {
    	zepto:"javascripts/zepto.min",
    	dingtalk:"javascripts/dingtalk",
        vue: "components/vue/vue.min",
        axios: "components/axios/axios.min",
        MINT: "components/mint-ui/index",
        search: "resources/js/search",
        common: "resources/js/common",
        wx: "http://res.wx.qq.com/open/js/jweixin-1.2.0",
        jssdk: "resources/js/jssdk"
    },
    shim: {
        "MINT": {
            deps: ["vue"],
            exports: "MINT"
        },
        "wx": {
            exports: "wx"
        }
    }
});

require(["zepto","dingtalk","vue", "axios", "common", "MINT", "search", "jssdk"], function (zepto,dingtalk,Vue, axios, common, MINT, search, jssdk) {

    Vue.component(MINT.Popup.name, MINT.Popup);
    Vue.component(MINT.Toast.name, MINT.Toast);

    window.MINT = MINT;

    var vm = new Vue({
        el: "#view",
        data: {
            detail: {},
            occurrence_frequency: [],
            incident_urgency: [],
            incident_effect: [],
            project: [],
            department: [],
            user_group: [],
            icd_category: {
                id: "",
                name: ""
            },
            supporter: {
                id: "",
                name: ""
            },
            submitVisible: false,
            department_select: "",
            users: [],
            bpm_commons: [],
            commons: "",
            suggestion: "",
            submitLine: {},
            readonly: false,
            others: {}
        },
        mounted: function () {
            var self = this;

            var id = common.getParam("id");
            this.readonly = common.getParam("readonly") == 1;

            axios.get("/incident/detail?id=" + id)
                .then(function (detail) {
                    axios.all([
                        axios.get("/common/dict?typeCode=OCCURRENCE_FREQUENCY"),
                        axios.get("/common/dict?typeCode=INCIDENT_URGENCY"),
                        axios.get("/common/dict?typeCode=INCIDENT_EFFECT"),
                        axios.get("/common/project?userId=" + detail.data.cus_user_id),
                    ]).then(axios.spread(function (occurrence_frequency, incident_urgency, incident_effect, project) {
                        self.occurrence_frequency = occurrence_frequency.data;
                        self.incident_urgency = incident_urgency.data;
                        self.incident_effect = incident_effect.data;
                        self.project = project.data;

                        self.detail = detail.data;
                        self.icd_category.id = detail.data.icd_category_id;
                        self.icd_category.name = detail.data.icd_category_name;
                        self.supporter.id = detail.data.supporter_id;
                        self.supporter.name = detail.data.supporter_name;
                    }));
                });

            axios.get("common/department")
                .then(function (response) {
                    self.department = response.data;
                });
        },
        methods: {
            commonsChange: function (event) {
                this.suggestion += event.target.value;
            },
            assignment: function (line) {
                if (common.isEmpty(this.icd_category.id)) {
                    MINT.Toast("请输入事件分类");
                    return;
                }
                if (common.isEmpty(this.detail.occurrence_frequency)) {
                    MINT.Toast("请输入发生频率");
                    return;
                }
                if (common.isEmpty(this.detail.urgency_name)) {
                    MINT.Toast("请输入紧急度");
                    return;
                }
                if (common.isEmpty(this.detail.effect_name)) {
                    MINT.Toast("请输入影响度");
                    return;
                }
                //if (common.isEmpty(this.detail.project_id)) {
                //    MINT.Toast("请输入归属项目");
                //    return;
                //}
                if (common.isEmpty(this.supporter.id)) {
                    MINT.Toast("请输入支持人员");
                    return;
                }

                var self = this;

                self.others.longitude = jssdk.location().longitude;
                self.others.latitude = jssdk.location().latitude;

                if (line.processMode == 2 || line.commitType != 0 || line.areOpenCommentWindow) {
                    this.submitLine = line;
                    if (line.processMode == 2 || (line.processMode == 0 && line.commitType == 1)) {
                        axios.get("/common/submitUser?key=incidentProcess&processId=" + line.processInstanceId + "&nextTaskId=" + line.nextTaskId + "&businessKey=" + this.detail.id + "&processMode=" + line.processMode)
                            .then(function (response) {
                                self.users = response.data;
                            });
                    }

                    axios.get("/common/dict?typeCode=BPM_COMMONS")
                        .then(function (response) {
                            self.bpm_commons = response.data;
                        });

                    this.submitVisible = true;
                } else {
                    var data = {
                        data: {
                            id: this.detail.id,
                            icdCategoryId: this.icd_category.id,
                            icdCategoryName: this.icd_category.name,
                            occurrenceFrequency: this.detail.occurrence_frequency,
                            urgencyName: this.detail.urgency_name,
                            effectName: this.detail.effect_name,
                            projectId: this.detail.project_id,
                            supporterId: this.supporter.id,
                            supporterName: this.supporter.name,
                            dealType: 2
                        },
                        line: line,
                        others: this.others
                    };
                    common.setColumn(data);

                    axios.post("/incident/submit", data)
                        .then(function (response) {
                            alert(response.data.message);
                            common.goback();
                        });
                }
            },
            popupSubmit: function () {
                if (this.users && common.isEmpty(this.nextAssigner)) {
                    MINT.Toast({ message: "请输入下一步处理人", position: "middle" });
                    return;
                }

                var self = this;

                var data = {
                    data: {
                        id: this.detail.id,
                        serviceCategoryId: this.icd_category.id,
                        serviceCategoryName: this.icd_category.name,
                        occurrenceFrequency: this.detail.occurrence_frequency,
                        urgencyName: this.detail.urgency_name,
                        effectName: this.detail.effect_name,
                        projectId: this.detail.project_id,
                        supporterId: this.supporter.id,
                        supporterName: this.supporter.name,
                        dealType: 2
                    },
                    nextAssigner: this.nextAssigner,
                    suggestion: this.suggestion,
                    line: this.submitLine,
                    others: this.others
                };
                common.setColumn(data);

                axios.post("/incident/submit", data)
                    .then(function (response) {
                        alert(response.data.message);
                        common.goback();
                    });
            },
            cancelSubmit: function () {
                this.submitLine = {};
                this.submitVisible = false;
            }
        }
    });
});